/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BATabSwitchSegmentViewController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 19/09/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BATabSwitchSegmentViewController.h"
#import "BAUtils.h"


@interface BATabSwitchSegmentViewController ()
@property (weak, nonatomic) IBOutlet UIView *selectedBackgroundView;
@property (weak, nonatomic) IBOutlet UIView *unselectedBackgroundView;
@property (weak, nonatomic) IBOutlet UIView *rightSpacerView;
@property (weak, nonatomic) IBOutlet UIImageView *unselectedBackgroundImageView;

@end

@implementation BATabSwitchSegmentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //_isSelected = TRUE;
        _index = 0;
        _selectedIndex = 0;
        _count = 1;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.unselectedBackgroundImageView setImage:[[UIImage imageNamed:@"tab-switch-unselected-innershadow"] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 1, 0)]];
    // Do any additional setup after loading the view from its nib.
    
    if (RD_IS_IPAD) {
        self.switchButton.titleLabel.font = BA_FONT_REGULAR(11);
    } else {
        BA_LABEL_SET_FONT_REGULAR(self.switchButton.titleLabel);
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)update
{
    
    if (self.index == self.selectedIndex) {
        //Selected
        [self.selectedBackgroundView setHidden:NO];
        [self.unselectedBackgroundView setHidden:YES];
        [self.leftShadowImage setHidden:YES];
        [self.rightShadowImage setHidden:YES];
        [self.rightSpacerView setHidden:YES];
    } else if (self.index == self.selectedIndex-1){
        //Selection right
        [self.selectedBackgroundView setHidden:YES];
        [self.unselectedBackgroundView setHidden:NO];
        [self.leftShadowImage setHidden:YES];
        [self.rightShadowImage setHidden:NO];
        [self.rightSpacerView setHidden:YES];
    } else if (self.index == self.selectedIndex+1){
        //Selection left
        [self.selectedBackgroundView setHidden:YES];
        [self.unselectedBackgroundView setHidden:NO];
        [self.leftShadowImage setHidden:NO];
        [self.rightShadowImage setHidden:YES];
        if (self.index != self.count-1) {
            [self.rightSpacerView setHidden:NO];
        } else{
            [self.rightSpacerView setHidden:YES];
        }
    } else{
        //Selection beyond
        [self.selectedBackgroundView setHidden:YES];
        [self.unselectedBackgroundView setHidden:NO];
        [self.leftShadowImage setHidden:YES];
        [self.rightShadowImage setHidden:YES];
        if (self.index != self.count-1) {
            [self.rightSpacerView setHidden:NO];
        } else{
            [self.rightSpacerView setHidden:YES];
        }
    }
}

-(void)setCount:(int)count{
    if (count != _count) {
        _count = count;
        [self update];
    }
}

-(void)setSelectedIndex:(int)selectedIndex{
    if (selectedIndex != _selectedIndex) {
        _selectedIndex = selectedIndex;
        [self update];
    }
}

-(void)setIndex:(int)index{
    if (index != _index) {
        _index = index;
        [self update];
    }
}

-(void)setSelectedColor:(UIColor *)selectedColor{
    _selectedColor = selectedColor;
    if (selectedColor != nil) {
        [self.selectedBackgroundView setBackgroundColor:self.selectedColor];
    }
    
}

-(void)setUnselectedColor:(UIColor *)unselectedColor{
    _unselectedColor = unselectedColor;
    if (unselectedColor != nil) {
        [self.unselectedBackgroundView setBackgroundColor:self.unselectedColor];
    }
    
}

-(void)setHideStartImage:(BOOL)hideStartImage
{
    _hideStartImage=hideStartImage;
    [self update];
}

-(void)setHideEndImage:(BOOL)hideEndImage
{
    _hideEndImage=hideEndImage;
    [self update];
}


@end
